#include "compi/serializer.h"

#include <gtest/gtest.h>

#include <cstddef>
#include <string>
#include <vector>

/**
 * 测试基本序列化功能的测试类
 * 验证 serializer 的基本打包和解包功能
 */
class PackedRangeTest : public ::testing::Test
{
protected:
    void SetUp() override
    {
        // 基本设置
    }

    void TearDown() override
    {
        // 清理
    }
};

/**
 * 测试基本类型的序列化和反序列化
 * 验证整数类型的序列化正确性
 */
TEST_F(PackedRangeTest, BasicIntegerSerialization)
{
    std::vector<int> original = {1, 2, 3, 4, 5};

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<int>>(buffer);

    EXPECT_EQ(original, deserialized);
}

/**
 * 测试浮点类型的序列化和反序列化
 * 验证浮点数的序列化正确性
 */
TEST_F(PackedRangeTest, FloatingPointSerialization)
{
    std::vector<double> original = {1.1, 2.2, 3.3, 4.4, 5.5};

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<double>>(buffer);

    EXPECT_EQ(original.size(), deserialized.size());
    for (size_t i = 0; i < original.size(); ++i)
    {
        EXPECT_DOUBLE_EQ(original[i], deserialized[i]);
    }
}

/**
 * 测试字符串类型的序列化和反序列化
 * 验证变长类型的序列化正确性
 */
TEST_F(PackedRangeTest, StringSerialization)
{
    std::vector<std::string> original = {"hello", "world", "test", "serialization"};

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<std::string>>(buffer);

    EXPECT_EQ(original, deserialized);
}

/**
 * 测试空容器的处理
 * 验证空向量的序列化和反序列化行为
 */
TEST_F(PackedRangeTest, EmptyContainerSerialization)
{
    std::vector<int> empty_original;

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(empty_original);
    auto deserialized = compi::deserialize<std::vector<int>>(buffer);

    EXPECT_EQ(empty_original, deserialized);
    EXPECT_TRUE(deserialized.empty());
}

/**
 * 测试嵌套容器的序列化和反序列化
 * 验证嵌套容器的序列化正确性
 */
TEST_F(PackedRangeTest, NestedContainerSerialization)
{
    std::vector<std::vector<int>> original = {
        {1, 2, 3}, {4, 5}, {6, 7, 8, 9}, {} // 空的内层容器
    };

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<std::vector<int>>>(buffer);

    EXPECT_EQ(original, deserialized);
}

/**
 * 测试大数据量的序列化和反序列化性能
 * 验证大容器的处理能力
 */
TEST_F(PackedRangeTest, LargeDataSerialization)
{
    const size_t large_size = 10000;
    std::vector<int> original(large_size);

    // 填充数据
    for (size_t i = 0; i < large_size; ++i)
    {
        original[i] = static_cast<int>(i);
    }

    // 序列化与反序列化
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<int>>(buffer);

    EXPECT_EQ(original, deserialized);
}

/**
 * 测试内存效率
 * 验证序列化后的数据大小合理性
 */
TEST_F(PackedRangeTest, MemoryEfficiency)
{
    std::vector<int> original = {1, 2, 3, 4, 5};

    // 序列化
    auto buffer = compi::serialize_to_byte_vector(original);

    // 序列化后的大小应该至少覆盖数据本体
    size_t expected_min_size = original.size() * sizeof(int);

    EXPECT_GE(buffer.size(), expected_min_size);
}

/**
 * 测试往返一致性（多次序列化反序列化）
 * 验证多次序列化操作的稳定性
 */
TEST_F(PackedRangeTest, RoundTripConsistency)
{
    std::vector<std::string> original = {"test", "round", "trip", "consistency"};

    auto data = original;

    // 进行多次序列化反序列化操作
    for (int i = 0; i < 5; ++i)
    {
        auto buffer = compi::serialize_to_byte_vector(data);
        data = compi::deserialize<std::vector<std::string>>(buffer);

        EXPECT_EQ(original, data);
    }
}

/**
 * 测试基本错误处理
 * 验证无效数据的处理
 */
TEST_F(PackedRangeTest, BasicErrorHandling)
{
    // 测试空缓冲区的反序列化
    std::vector<std::byte> empty_buffer;

    // 尝试从空缓冲区反序列化应该抛出异常
    EXPECT_THROW(compi::deserialize<std::vector<int>>(empty_buffer), std::exception);
}

/**
 * 测试字节序一致性
 * 验证不同字节序设置下的兼容性
 */
TEST_F(PackedRangeTest, ByteOrderConsistency)
{
    std::vector<uint32_t> original = {0x12345678, 0xABCDEF00, 0x11223344};

    // 序列化与反序列化（默认小端）
    auto buffer = compi::serialize_to_byte_vector(original);
    auto deserialized = compi::deserialize<std::vector<uint32_t>>(buffer);

    EXPECT_EQ(original, deserialized);

    // 验证字节序处理
    for (size_t i = 0; i < original.size(); ++i)
    {
        EXPECT_EQ(original[i], deserialized[i]);
    }
}